var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var area = require('../../../utils/area.js');
import WxValidate from '../../../utils/WxValidate';

var app = getApp();
Page({
  data: {
    address: {
      id: 0,
      areaCode: 0,
      address: '',
      name: '',
      tel: '',
      isDefault: 0,
      county: ''
    },
    addressId: 0
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      name: {
        required: true,
      },
      tel: {
        required: true,
        tel: true,
      },
      county: {
        required: true,
      },
      addressDetail: {
        required: true,
      }
    }
    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      name: {
        required: '请输入姓名',
      },
      tel: {
        required: '请输入手机号',
        tel: '请输入正确手机号',
      },
      county: {
        required: '请选择送货地址',
      },
      addressDetail: {
        required: '请输入具体地址',
      }
    }
    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages);
  },
  loadLocation: function() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              //统一
              wx.chooseLocation({
                // type: 'gcj02', //返回可以用于wx.openLocation的经纬度
                success: function(res) {
                  // var latitude = res.latitude //维度
                  // var longitude = res.longitude //经度
                  // console.log(res);
                  let address = that.data.address;
                  address.county = res.address + res.name;
                  that.setData({
                    address: address
                  });
                }
              })
            }
          })
        } else {
          wx.chooseLocation({
            // type: 'gcj02', //返回可以用于wx.openLocation的经纬度
            success: function(res) {
              let address = that.data.address;
              address.county = res.address + res.name;
              that.setData({
                address: address
              });
            }
          })
        }
      }
    });
  },
  bindinputMobile(event) {
    let address = this.data.address;
    address.tel = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputName(event) {
    let address = this.data.address;
    address.name = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindinputAddress(event) {
    let address = this.data.address;
    address.addressDetail = event.detail.value;
    this.setData({
      address: address
    });
  },
  bindIsDefault() {
    let address = this.data.address;
    address.isDefault = !address.isDefault;
    this.setData({
      address: address
    });
  },
  getAddressDetail() {
    let that = this;
    util.request(api.AddressDetail, {
      id: that.data.addressId
    }).then(function(res) {
      if (res.code === 0) {
        if (res.data) {
          that.setData({
            address: res.data
          });
        }
      }
    });
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options)
    if (options.id && options.id != 0) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    }
    this.initValidate()
  },
  cancelAddress() {
    wx.navigateBack();
  },
  submitForm(e) {
    // 传入表单数据，调用验证方法
    if (!this.WxValidate.checkForm(e.detail.value)) {
      const error = this.WxValidate.errorList[0]
      util.showErrorToast(error.msg);
      return false;
    }
    wx.showLoading({
      title: '加载中...',
      mask: false
    })

    let that = this,
      address = this.data.address;
    util.request(api.AddressSave, {
      id: address.id,
      name: address.name,
      tel: address.tel,
      county: address.county,
      areaCode: address.areaCode,
      addressDetail: address.addressDetail,
      isDefault: address.isDefault
    }, 'POST').then(function(res) {
      wx.hideLoading()
      if (res.code === 0) {
        //返回之前，先取出上一页对象，并设置addressId
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        console.log(prevPage);
        if (prevPage.route == "pages/checkout/checkout") {
          prevPage.setData({
            addressId: res.data
          })

          try {
            wx.setStorageSync('addressId', res.data);
          } catch (e) {

          }
          console.log("set address");
        }
        wx.navigateBack();
      }
    });
  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  }
})